<template>
  <div class="stepBox">
    <div class="cont">
      <batch-operat-btn
        v-if="editable"
        :isView="false"
        :addData="true"
        addText='选择商品'
        @onAddData="onAddData"
      >
        <template #cusBtn> </template>
      </batch-operat-btn>
      <div class="recordBox">
        <a-list-table
          :columns="columns"
          :inputName="inputName"
          :tableData="listdata"
          :isSelecttion="false"
          :loading="loading"
          :scroll="{ y: null }"
          @onAction="onAction"
          @onChangeInput="onChangeInput"
        >
        </a-list-table>
      </div>
    </div>
    <div
      class="btnBox"
      v-if="isNew"
    >
      <a-button
        class="btn"
        style="marginRight:20px"
        type="default"
        @click="previous()"
      >
        上一步
      </a-button>
      <a-button
        class="btn"
        type="primary"
        @click="nexts()"
      >
        提交
      </a-button>
    </div>
    <addGoodsModal
      ref="addGoodsModal"
      @onAddInfo="onAddInfo"
    />
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import addGoodsModal from './addGoodsModal'
// import { getInfoByPid as getSupplierInfoByPid } from '@/views/app/biz/supplier/supplier'
import { getLicenseByPid, saveOederGoods } from './saleOrder'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    addGoodsModal,
  },
  props: {
    orderData: {
      type: Object,
    }
  },
  data () {
    return {
      footer: undefined,
      editable: true,
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '添加商品',
        modalWidth: '1000px',
      },
      columns: [
        { title: '品名', dataIndex: 'productNameZh', key: 'productNameZh', align: 'center', },
        // { title: '通用名', dataIndex: 'commonName', key: 'commonName', align: 'center' },
        { title: '销售单号', dataIndex: 'saleOrderNo', key: 'saleOrderNo', align: 'center', },
        { title: '生产企业名称', dataIndex: 'manufacturerZh', key: 'manufacturerZh', align: 'center', },
        { title: '规格', dataIndex: 'spec', key: 'spec', align: 'center' },
        { title: '型号', dataIndex: 'model', key: 'model', align: 'center' },
        { title: '计量单位', dataIndex: 'measurement', key: 'measurement', align: 'center' },
        { title: '单品含税价', dataIndex: 'iunitprice', key: 'iunitprice', align: 'center', scopedSlots: { customRender: 'iunitprice' } },
        { title: '采购数量', dataIndex: 'num', key: 'num', align: 'center', scopedSlots: { customRender: 'num' } },
        {
          title: '操作',
          dataIndex: 'action',
          width: 100,
          key: 'operation',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      inputName: ['iunitprice', 'num'],
      isNew: true,
      listdata: [],
      listKey: 0,
      changeList: [],
      pid: '',
      sysSeq: 0,
      loadings: false,
      order: {}
    }
  },
  computed: {},
  mounted () { },
  watch: {
    orderData: {
      handler: function (val, oldVal) {
        console.log(this.orderData);
        this.pid = this.orderData.pid
        this.sysSeq = this.orderData.sysSeq
        this.order = this.orderData
      },
      deep: true,
      immediate: true
    }
  },
  methods: {

    // 关闭模态详细框
    closeModal () {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    //点击选择商品
    onAddData () {
      this.$refs.addGoodsModal.addModal(this.pid)
    },
    // 子modal提交过来的信息
    onAddInfo (datas) {
      let existed = false
      // this.listdata.find((item) => {
      //   if (datas.beiShouqdw == item.beiShouqdw) {
      //     existed = true
      //   }
      // })
      if (!existed) {
        for (let item of datas) {
          console.log(this.listdata);
          for (let items of this.listdata) {
            console.log(this.listdata, items.key, item.pid);
            if (items.saleGoodsPid == item.pid) {
              this.$message.warn({ content: '商品：' + item.pinMing + '已添加，请勿重复添加' })
              return
            }
          }
          this.listdata.push({
            key: item.pid,
            action: ['删除'],
            sysSeq: 0,
            saleOrderPid: this.order.pid,
            saleOrderNo: this.order.saleOrderNo,
            saleGoodsPid: item.pid,
            productNameZh: item.pinMing,
            model: item.xingHao,
            spec: item.guiGe,
            manufacturerZh: item.shengChanqymc,
            measurement: item.jiLiangdw,
            num: item.zuiXiaocgl ? item.zuiXiaocgl : 0,
            iunitprice: item.hanShuidj,
          })
          this.$message.success({ content: '商品：' + item.pinMing + '已成功添加' })
        }
        this.$refs.addGoodsModal.closeModal()
      } else {
        this.$message.warn({ content: '已添加该商品' })
      }
      console.log(this.listdata)
    },
    previous () {
      this.$emit("onCancel")
    },
    skip () {
      this.$emit("onSkip", 4)
    },
    nexts () {
      if (this.listdata.length) {
        let listdata = JSON.parse(JSON.stringify(this.listdata))
        for (const item of listdata) {
          delete item.action;
        }
        // this.$emit('onSubmit')
        this.saveObjInfo(listdata)
      } else {
        this.$message.warn({ content: '暂未添加任何商品', duration: 2 })
      }
    },
    //修改列表信息aList为修改的记录
    onChangeInput (aList) {
    },
    // 保存详细信息
    async saveObjInfo (val) {
      console.log(val)
      let key = 'updatable'
      this.$message.loading({ content: '提交中', key, duration: 0 })
      let res = await saveOederGoods(val)
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', key, duration: 3 })
        this.closeModal()
        this.$emit('onSubmit')
      } else {
        this.$message.error({ content: '提交失败,请稍后再试', key, duration: 3 })
      }
    },
    onAction (text, record, index) {
      console.log(text, record, index)
      if (text == '删除') {
        this.listdata.splice(index, 1)
      }
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
.stepBox {
  width: 100%;
  height: 100%;
  position: relative;
}
.cont {
  padding-left: 5px;
  max-height: calc(70vh - 40px);
  overflow-y: scroll;
}
.btnBox {
  height: 7vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  /* border: 1px solid #eee; */
  position: absolute;
  bottom: 0px;
}
</style>